package cn.com.lyb.flink.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class KafkaSourceDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        // kafka 命令：sh /opt/cloudera/parcels/CDH-6.2.0-1.cdh6.2.0.p0.967373/bin/kafka-console-producer --broker-list 172.32.1.171:9092 --topic topic_1

        ///opt/cloudera/parcels/CDH-6.2.0-1.cdh6.2.0.p0.967373/bin
        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers("172.32.1.171:9092")
                .setGroupId("flink")
                .setTopics("topic_1")
                .setValueOnlyDeserializer(new SimpleStringSchema())// 序列化，只对值序列化， .setDeserializer() k-v 都序列化
                .setStartingOffsets(OffsetsInitializer.latest())// kafka 消费策略
                .build();
        env.fromSource(kafkaSource,
                WatermarkStrategy.noWatermarks(),
                "kafka").print();
        env.execute();
        /*
        kafka消费者的参数:auto.reset.offsets
            earliest: 如果有offset，从offset继续消费;如果没有offset，从最早消费
            latest: 如果有offset，从offset继续消费;如果没有offset，从最新消费Latest:
        flink的kafkasource,offset消费策略:OffsetsInitializer，默认为earliest
         earliest 一定从 最早 消费:
         latest   一定从 最新 消费

         */
    }
}
